package br.com.clebao.droidcontroler.persistencia;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import br.com.clebao.droidcontroler.modelo.Usuario;

public class DAOUsuario extends DAODroid<Usuario> {
	

	public DAOUsuario(Context c) {
		super(c);
		setTableName();
	}

	@Override
	public long addObject(Usuario obj) {
		ContentValues valores = new ContentValues();
		valores.put("nome", obj.getNome());
		valores.put("login", obj.getLogin());
		valores.put("senha", obj.getSenha());
		valores.put("Id_tipo_usuario", obj.getTipoUsuario());
		valores.put("id_filial", obj.getFilial().getId());
		return openDB().insert(tableName, null, valores);
		
	}

	@Override
	public boolean updateObject(Usuario obj) {
		ContentValues valores = new ContentValues();
		valores.put("nome", obj.getNome());
		valores.put("login", obj.getLogin());
		valores.put("senha", obj.getSenha());
		valores.put("Id_tipo_usuario", obj.getTipoUsuario());
		valores.put("id_filial", obj.getFilial().getId());
		return openDB().update(tableName, valores, "_id = " + obj.getId(), null) > 0;
		
	}
	
	public boolean loginUsuario(String login, String senha){
		String[] ValorFiltro = new String[]{login, senha};
		
		
		Cursor c = openDB().query(tableName, null, "login = ? and senha = ?", ValorFiltro, null, null, null);
		
		boolean toReturn = c.moveToFirst();
		
		c.close();
		db.close();
		
		return toReturn;
	}
	
	public boolean deleteObject(int id) {
		return openDB().delete(tableName, "_id = " + id, null) > 0;
	}

	public Cursor findObject(int id) {		
		return openDB().query(tableName, null, "_id = " + id, null, null, null, null);
	}

	public Cursor findAllObject() {
		
		return openDB().query(tableName,null, null, null, null, null, "_id");
	}

	public void setTableName() {
		this.tableName = "usuario";
	}



	

}
